<?php
/**
 * MageWorx
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the MageWorx EULA that is bundled with
 * this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.mageworx.com/LICENSE-1.0.html
 *
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@mageworx.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade the extension
 * to newer versions in the future. If you wish to customize the extension
 * for your needs please refer to http://www.mageworx.com/ for more information
 * or send an email to sales@mageworx.com
 *
 * @category   MageWorx
 * @package    MageWorx_DeliveryZone
 * @copyright  Copyright (c) 2009 MageWorx (http://www.mageworx.com/)
 * @license    http://www.mageworx.com/LICENSE-1.0.html
 */

/**
 * Delivery Zone extension
 *
 * @category   MageWorx
 * @package    MageWorx_DeliveryZone
 * @author     MageWorx Dev Team <dev@mageworx.com>
 */

class MageWorx_DeliveryZone_Helper_Data extends Mage_Core_Helper_Abstract
{
    protected $_location = null;

    public function setLocation(array $data)
    {
        $data = array_intersect_key($data, array('country_id' => '', 'region_id' => '', 'region' => ''));
        $cookie = Mage::getSingleton('core/cookie');
        $cookie->set('shipping_region', rawurlencode(base64_encode(serialize($data))), true, '/', null, null, true);
        $this->_location = new Varien_Object();
        $this->_location->setData($data);
        $this->_setLocationDetails();
    }

    public function getLocation()
    {
        if (!$this->_location instanceof Varien_Object){
            $data = $this->_getRequest()->getCookie('shipping_region', null);
            if ($data){
                $data = unserialize(base64_decode(rawurldecode($data)));
            }
            $this->_location = new Varien_Object();
            $this->_location->setData($data);
            $this->_setLocationDetails();
        }
        return $this->_location;
    }

    public function getIpLocation()
    {
    	$countryCode = Mage::helper('ipcountry')->getCountryCode();
    	return $countryCode;
    }
    private function _setLocationDetails()
    {
        if ($countryId = $this->_location->getCountryId()){
            $country = Mage::getModel('directory/country')->load($countryId);
            $this->_location->setCountryName($country->getName());
        }
        if ($regionId = $this->_location->getRegionId()){
            $region = Mage::getModel('directory/region')->load($regionId);
            $this->_location->setRegionName($region->getName());
        }
        elseif ($region = $this->_location->getRegion()){
            $this->_location->setRegionName($region);
            $this->_location->setRegionId('');
        }
    }

    public function isLocationEqual(array $data)
    {
        $location = new Varien_Object();
        $location->setData($data);
        $zoneLocation = $this->getLocation();
        if ($zoneLocation->getCountryId() == $location->getCountryId() &&
        	$zoneLocation->getRegionId() == $location->getRegionId()) {
            return true;
        }
        return false;
    }
}